﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormsApplication1.UserInfo;
using System.Net;
using Microsoft.Dynamics.BusinessConnectorNet;
using System.ServiceModel;
using System.Security.Principal;

namespace WindowsFormsApplication1
{
    public partial class MainForm : Form
    {
        private List<UserGroup> userGroups = new List<UserGroup>();
        private List<Company> companies = new List<Company>();
        public MainForm()
        {
            InitializeComponent();
        }

        private void btCreateUser_Click(object sender, EventArgs e)
        {
            ServiceSoapClient client = new ServiceSoapClient();
            try
            {
                client.ClientCredentials.Windows.ClientCredential = new System.Net.NetworkCredential();
                if (txtEndpoint.Text !="")
                {
                    client.Endpoint.Address = new EndpointAddress(txtEndpoint.Text);
                }
                string userGroupsString = string.Empty;
                foreach(int indexChecked in checkedListBox1.CheckedIndices) 
                {
                    userGroupsString += checkedListBox1.Items[indexChecked].ToString() + ",";
                }

                var result = client.CreateUserTool(txtUserId.Text, txtAlias.Text, txtUserName.Text, txtExternalName.Text, txtDomain.Text, txtEmail.Text, userGroupsString, cbCompanies.Text);

                if (result.ToString() == "Success")
                {
                    txtResult.Text = "User " + txtUserId.Text + " created";
                }
                else
                {
                    throw new Exception(result);
                }



            }
            catch (Exception exc)
            {
                txtResult.Text = exc.Message;
            }
            finally
            {
                client.Close();
            }
            
           

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox5.Text !=string.Empty)
            {
                ServiceSoapClient client = new ServiceSoapClient();
                try
                {
                    client.ClientCredentials.Windows.ClientCredential = new System.Net.NetworkCredential();
                    if (txtEndpoint.Text != "")
                    {
                        client.Endpoint.Address = new EndpointAddress(txtEndpoint.Text);
                    }
                    var result = client.GetUserInfo(textBox5.Text);

                    textBox4.Text = result.userName;
                    textBox3.Text = result.userAlias;
                    textBox2.Text = result.userDomain;
                    textBox1.Text = result.userCompany;

                }
                catch (Exception exc)
                {
                    txtResult.Text = exc.Message;
                    MessageBox.Show(exc.Message);
                }
                finally
                {
                    client.Close();
                }
            
            
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox6.Text != string.Empty)
            {
                ServiceSoapClient client = new ServiceSoapClient();
                try
                {
                    client.ClientCredentials.Windows.ClientCredential = new System.Net.NetworkCredential();
                    if (txtEndpoint.Text != "")
                    {
                        client.Endpoint.Address = new EndpointAddress(txtEndpoint.Text);
                    }
                    var result = client.DeleteUser(textBox6.Text);

                    if (result)
                    {
                        MessageBox.Show("User deleted");
                        textBox6.Text = string.Empty;
                    }
                    else
                    {
                        MessageBox.Show("User not deleted.");
                    }
                }
                catch (Exception exc)
                {
                    txtResult.Text = exc.Message;
                    MessageBox.Show(exc.Message);
                }
                finally
                {
                    client.Close();
                } 
            }
        }

        

        private void button3_Click_1(object sender, EventArgs e)
        {

            ServiceSoapClient client = new ServiceSoapClient();
            try
            {
                client.ClientCredentials.Windows.ClientCredential = new System.Net.NetworkCredential();
                if (txtEndpoint.Text != "")
                {
                    client.Endpoint.Address = new EndpointAddress(txtEndpoint.Text);
                }
                //var result = client.CreateUserTool("scos", "scoshciug5", "scoshciug5", "scoshciug5", "ellipse-md.com", "scoshciug@ff.tu");

                //MessageBox.Show(result);
            }
            catch (Exception exc)
            {
                txtResult.Text = exc.Message;
                MessageBox.Show(exc.Message);
            }
            finally
            {
                client.Close();
            } 

        }

        private void button4_Click(object sender, EventArgs e)
        {
            ServiceSoapClient client = new ServiceSoapClient();
            try
            {
                client.ClientCredentials.Windows.ClientCredential = new System.Net.NetworkCredential();
                if (txtEndpoint.Text != "")
                {
                    client.Endpoint.Address = new EndpointAddress(txtEndpoint.Text);
                }
                var result = client.GetUserGoups();
                StringBuilder sb = new StringBuilder();

                foreach (var userGroup in result)
                {
                    sb.AppendLine(userGroup.userGroupId);               
                }

                txtUserGroups.Text = sb.ToString();
                MessageBox.Show("Completed");
            }
            catch (Exception exc)
            {
                txtResult.Text = exc.Message;
                MessageBox.Show(exc.Message);
            }
            finally
            {
                client.Close();
            } 

        }

        private List<UserGroup> GetUserGroups()
        {
            ServiceSoapClient client = new ServiceSoapClient();
            List<UserGroup> localUserGroups = new List<UserGroup>();
            try
            {
                client.ClientCredentials.Windows.ClientCredential = new System.Net.NetworkCredential();
                if (txtEndpoint.Text != "")
                {
                    client.Endpoint.Address = new EndpointAddress(txtEndpoint.Text);
                }
                var result = client.GetUserGoups();
                StringBuilder sb = new StringBuilder();

                foreach (var userGroup in result)
                {
                    localUserGroups.Add(userGroup);
                }

                txtUserGroups.Text = sb.ToString();
            }
            catch (Exception exc)
            {
                throw new Exception("Load user groups exception:" + exc.Message);
            }
            finally
            {
                client.Close();
            }

            return localUserGroups;
            
        }

        private List<Company> GetCompanies()
        {
            ServiceSoapClient client = new ServiceSoapClient();
            List<Company> localCompanies = new List<Company>();
            try
            {
                client.ClientCredentials.Windows.ClientCredential = new System.Net.NetworkCredential();
                if (txtEndpoint.Text != "")
                {
                    client.Endpoint.Address = new EndpointAddress(txtEndpoint.Text);
                }
                var result = client.GetCompanies();

                foreach (var company in result)
                {
                    localCompanies.Add(company);
                }

            }
            catch (Exception exc)
            {
                throw new Exception("Load companies exception:" + exc.Message);
            }
            finally
            {
                client.Close();
            }

            return localCompanies;

        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            txtUserId.Enabled = false;
            txtUserName.Enabled = false;
            txtAlias.Enabled = false;
            txtDomain.Enabled = false;
            checkedListBox1.Enabled = false;
            txtExternalName.Enabled = false;
            txtEmail.Enabled = false;

        }

        private void button5_Click(object sender, EventArgs e)
        {
            //Load user groups
            userGroups = GetUserGroups();
            foreach (var group in userGroups)
            {
                checkedListBox1.Items.Add(group.userGroupId);
            }
            companies = GetCompanies();

            foreach (var company in companies)
            {
                cbCompanies.Items.Add(company.companyId);
            }

            txtUserId.Enabled = true;
            txtUserName.Enabled = true;
            txtAlias.Enabled = true;
            txtDomain.Enabled = true;
            checkedListBox1.Enabled = true;
            txtExternalName.Enabled = true;
            txtEmail.Enabled = true;
        }
    }
}
